package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.truck.TruckDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.truck.PdTruck;
import com.itheima.pinda.mapper.PdTruckMapper;
import com.itheima.pinda.mapstruct.TruckDTO2Entity;
import com.itheima.pinda.service.PdTruckService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PdTruckServiceImpl extends ServiceImpl<PdTruckMapper, PdTruck> implements PdTruckService {

    @Resource
    private TruckDTO2Entity truckDTO2Entity;


    /**
     * 添加车辆
     *
     * @param dto 车辆信息
     * @return 车辆信息
     */
    @Override
    public TruckDto saveTruck(TruckDto dto) {
        PdTruck pdTruck = truckDTO2Entity.conver(dto);
        super.save(pdTruck);
        return dto;
    }

    /**
     * 获取车辆分页数据
     *
     * @param page         页码
     * @param pageSize     页尺寸
     * @param truckTypeId  车辆类型id
     * @param licensePlate 车牌号码
     * @param fleetId      所属车队Id
     * @return 车辆分页数据
     */
    @Override
    public PageResponse<TruckDto> findByPage(Integer page, Integer pageSize,
                                             String truckTypeId, String licensePlate, String fleetId) {

        Page<PdTruck> pdTruckPage = super.page(new Page<>(page, pageSize), Wrappers.<PdTruck>lambdaQuery()
                .eq(StringUtils.isNotBlank(truckTypeId), PdTruck::getTruckTypeId, truckTypeId)
                .like(StringUtils.isNotBlank(licensePlate), PdTruck::getLicensePlate, licensePlate)
                .eq(StringUtils.isNotBlank(fleetId), PdTruck::getFleetId, fleetId)
                .eq(PdTruck::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByDesc(PdTruck::getId));

        return PageResponse.<TruckDto>builder()
                .page(page)
                .pagesize(pageSize)
                .pages(pdTruckPage.getPages())
                .counts(pdTruckPage.getTotal())
                .items(pdTruckPage.getRecords()
                        .stream()
                        .map(item -> truckDTO2Entity.conver(item))
                        .collect(Collectors.toList()))
                .build();
    }


    /**
     * 获取车辆列表
     *
     * @param ids 车辆id列表
     * @return 车辆列表
     */
    @Override
    public List<TruckDto> findAll(List<String> ids, String fleetId) {
        List<PdTruck> pdTruckList = super.list(Wrappers.<PdTruck>lambdaQuery()
                .in(ids != null && ids.size() > 0, PdTruck::getId, ids)
                .eq(StringUtils.isNotBlank(fleetId), PdTruck::getFleetId, fleetId)
                .eq(PdTruck::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByDesc(PdTruck::getId));

        return pdTruckList.stream()
                .map(item -> truckDTO2Entity.conver(item))
                .collect(Collectors.toList());
    }

    /**
     * 删除车辆
     *
     * @param id 车辆id
     */
    @Override
    public void disableById(String id) {
        // TODO 检测车辆当前状态, 如果处于非空闲状态, 则不允许删除
        PdTruck pdTruck = new PdTruck();
        pdTruck.setId(Long.parseLong(id));
        pdTruck.setStatus(Constant.DATA_DISABLE_STATUS.byteValue());
        baseMapper.updateById(pdTruck);
    }
}
